package com.cduestc.mylogin;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SelfMessage extends AppCompatActivity {

    TextView encrypted,showBirthday,backHome;
    Button bind,btnOpenDig;
    EditText inputEmail,inputPhone;
    int nyear,nmonth,nday;
    static int recodeage = 0;
     int dateY;
     int dateM;
     int dateD;
    //DatePicker birthday;
    String birth;
    SharedPreferences share;

    //邮箱格式验证
    public static boolean isEmail(String email)
    {
        if (email == null || email.equals(""))
        {
            return false;
        }
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");   //简单匹配
        Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");    //复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    //手机号格式验证
    public boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个
        //"\\d{9}"代表后面是可以是0～9的数字，有9位
        String telRegex = "[1][34578]\\d{9}";
        if (TextUtils.isEmpty(mobiles)){
            return false;
        }else {
            return mobiles.matches(telRegex);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message);

        encrypted = findViewById(R.id.encrypted);
        showBirthday = findViewById(R.id.showBirthday);
        backHome = this.findViewById(R.id.backHome);
        bind = findViewById(R.id.bind);
        btnOpenDig = findViewById(R.id.btnOpenDig);
        inputEmail = findViewById(R.id.inputEmail);
        inputPhone = this.findViewById(R.id.inputPhone);
        Calendar calendar = Calendar.getInstance();
        nyear = calendar.get(Calendar.YEAR);
        nmonth = calendar.get(Calendar.MONTH);
        nday = calendar.get(Calendar.DAY_OF_MONTH);
        //birthday = findViewById(R.id.birthday);
        dateY = nyear;
        dateM = nmonth + 1;
        dateD = nday;
        //System.out.println(dateY + "," + dateM + "," + dateD);
        share = this.getSharedPreferences("selfMsg",MODE_PRIVATE);

        String el = share.getString("email","");
        String pe = share.getString("phone","");
        String bh = share.getString("birth","");

        if(!el.equals("")){
            inputEmail.setText(el);
        }
        if(!pe.equals("")){
            inputPhone.setText(pe);
        }
        if(!bh.equals("")){
            showBirthday.setText(bh);
        }

        backHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SelfMessage.this,HomePage.class);
                startActivity(intent);
            }
        });

        btnOpenDig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog dlg = new DatePickerDialog(SelfMessage.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        showBirthday.setText("");
                        nyear = i;
                        nmonth = i1 + 1;
                        nday = i2;

                        if(nyear > dateY) {
                            Toast.makeText(SelfMessage.this, "出生日期不应该大于当前日期", Toast.LENGTH_SHORT).show();
                            return;
                        }else if(nyear == dateY){
                            if(nmonth > dateM){
                                Toast.makeText(SelfMessage.this, "出生日期不应该大于当前日期", Toast.LENGTH_SHORT).show();
                                return;
                            }else if(nmonth == dateM){
                                if(nday > dateD){
                                    Toast.makeText(SelfMessage.this, "出生日期不应该大于当前日期", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                            }
                            recodeage = 0;
                        }else
                        if(nmonth <= dateM){
                            recodeage = dateY - nyear;
                        }else{
                            recodeage = dateY - nyear - 1;
                        }
                        birth = nyear + "/" + nmonth + "/" + nday;
                        showBirthday.setText(birth);
                        //Toast.makeText(SelfMessage.this,birth,Toast.LENGTH_SHORT).show();
                    }
                },nyear,nmonth,nday);
                dlg.show();

            }

        });
        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = inputEmail.getText().toString().trim();
                String b = showBirthday.getText().toString().trim();
                String phone = inputPhone.getText().toString().trim();

                if(email.equals("") && phone.equals("")){
                    Toast.makeText(SelfMessage.this,"邮箱和手机号至少绑定一项",Toast.LENGTH_SHORT).show();
                    return;
                }else if(!email.equals("") && !phone.equals("")){
                    if(isEmail(email) == false){
                        Toast.makeText(SelfMessage.this, "邮箱格式不合法，请重新输入", Toast.LENGTH_SHORT).show();
                        inputEmail.setText("");
                        return;
                    }
                    if(isMobileNO(phone) == false){
                        Toast.makeText(SelfMessage.this, "手机号格式不合法,请重新输入", Toast.LENGTH_SHORT).show();
                        inputPhone.setText("");
                        return;
                    }
                }
                if(isEmail(email) == false && isMobileNO(phone) == false){
                    Toast.makeText(SelfMessage.this,"邮箱/手机号格式不合法，请重新输入",Toast.LENGTH_SHORT).show();
                    inputEmail.setText("");
                    inputPhone.setText("");
                    return;
                }else if(b.equals("")){
                    System.out.println("test");
                    Toast.makeText(SelfMessage.this,"请选择你的生日",Toast.LENGTH_SHORT).show();
                    return;
                }else{

                    SharedPreferences.Editor seMsg = share.edit();
                    seMsg.putString("email",email);
                    seMsg.putString("birth",b);
                    seMsg.putString("phone",phone);
                    seMsg.commit();

                    Intent intent = new Intent();
                    Bundle bun = new Bundle();
                    bun.putString("email",email);
                    bun.putString("birthday",birth);
                    bun.putString("phone",phone);
                    intent.putExtras(bun);
                    SelfMessage.this.setResult(2001,intent);
                    Toast.makeText(SelfMessage.this, "信息绑定成功！", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }
}